# auther：wanghao
import time

from case.base.basepage import BasePage
from case.base import route
from case.pages.storepage import StorePage


class LivingPage(BasePage):
    """
    首页直播tab公共方法
    """
    locators = {
        "BASE_LIVING_IMAGE": "image.avatar-index--image",
        "BASE_LIVING_FLOW_IMAGE": "image.v3-index--cover-img"
    }

    """
    首页进入直播页面
    """

    def to_living_page(self):
        self.mini.page.get_element("view.index--tab-bar-item.index--index-1").click()

    """
    首页进入直播页面
    """

    def to_follow_living_page(self):
        self.mini.page.get_element("view.navigation-bar-index--item.navigation-bar-index--item-0").click()

    """
    判断从首页点击直播模块，判断跳转路径是否正确
    """

    def check_living_page(self):
        self.mini.assertEqual(self.current_path(), route.livingpage_route)

    """
    判断从直播-关注模块，判断跳转路径是否正确
    """

    def check_living_follow_page(self):
        self.mini.assertEqual(self.current_path(), route.livingpage_follow_route)

    """
    直播模块元素check
    """

    def check_living_element(self):
        self.mini.assertTrue(self.mini.page.element_is_exists(LivingPage.locators['BASE_LIVING_FLOW_IMAGE']))

    """
    通过直播瀑布卡片进入直播间
    """

    def into_living_from_card(self):
        living_status = self.mini.page.wait_for("view.v2-index--type.v2-index--status--playing", 3)
        if living_status:
            # 点击【直播中】元素进入直播间
            living_status_element = self.mini.page.get_element("view.v2-index--type.v2-index--status--playing")
            living_status_element.click()
            time.sleep(5)
            # check点赞元素
            self.mini.assertTrue(self.mini.page.element_is_exists("view.icons-index--icon-like", 5))
            # check转发元素
            self.mini.assertTrue(self.mini.page.element_is_exists("view.icons-index--icon-share"))
            # check优惠券元素
            self.mini.assertTrue(self.mini.page.element_is_exists("view.icons-index--icon-coupon"))
            # check其他更多元素
            self.mini.assertTrue(self.mini.page.element_is_exists("view.icons-index--icon-ellipsis"))
            # check发言元素
            self.mini.assertTrue(self.mini.page.element_is_exists("view.icons-index--comment.van-ellipsis"))
            # check宝贝袋元素
            self.mini.assertTrue(self.mini.page.element_is_exists("view.icons-index--goods-bag"))

    """
    购买【正在讲解】的商品
    """

    def buy_showing_good(self):
        # 点击立即购买
        if self.mini.page.wait_for("view.shopping-bar-index--hot-goods-item__right-button"):
            self.mini.page.get_element("view.shopping-bar-index--hot-goods-item__right-button").click()
        else:
            self.into_good_detail()

    """
    通过直播吸顶icon进入直播间
    """

    def into_living_from_icon(self):
        living_status = self.mini.page.wait_for("view.avatar-index--living-badge", 3)
        if living_status:
            # 点击【直播中】元素进入直播间
            living_status_element = self.mini.page.get_element("view.avatar-index--living-badge")
            living_status_element.click()
            time.sleep(3)
            # check点赞元素
            self.mini.assertTrue(self.mini.page.element_is_exists("view.icons-index--icon-like"))
            # check转发元素
            self.mini.assertTrue(self.mini.page.element_is_exists("view.icons-index--icon-share"))
            # check优惠券元素
            self.mini.assertTrue(self.mini.page.element_is_exists("view.icons-index--icon-coupon"))
            # check其他更多元素
            self.mini.assertTrue(self.mini.page.element_is_exists("view.icons-index--icon-ellipsis"))
            # check发言元素
            self.mini.assertTrue(self.mini.page.element_is_exists("view.icons-index--comment.van-ellipsis"))
            # check宝贝袋元素
            self.mini.assertTrue(self.mini.page.element_is_exists("view.icons-index--goods-bag"))

    """
    直播间跳转商详页
    """

    def into_good_detail(self):
        # 打开宝贝袋
        goods_bag_element = self.mini.page.get_element("view.icons-index--goods-bag")
        goods_bag_element.click()
        # 获取商品名称
        good_title_element = self.mini.page.get_element("view.popup-goods-item-index--goods-title.van-multi-ellipsis--l2")
        good_name = good_title_element.inner_text
        # 点击第一个商品进入商详页
        good_title_element.click()
        time.sleep(2)
        good_detail_title_element = self.mini.page.get_element("view.title-index--goods-card-title.van-ellipsis.new-goods-card-index--goods-card-new__title")
        good_name_detail = good_detail_title_element.inner_wxml
        assert good_name.replace(' ','') == good_name_detail.replace(' ','')
        self.mini.assertEqual(self.current_path(), route.goods_detail)

    """
    直播间商详页购买商品
    """

    def buy_goods_from_detail(self):
        # sku选择面板
        self.select_sku_model()
        # 商详页发起购买
        buy_button = self.mini.page.get_element("button.operation-index--e-core-btn.operation-index--e-core-btn__main")
        buy_button.click()
        return self

    """
    sku选择面板
    """

    def select_sku_model(self):
        # sku选择面板
        sku_flag = self.mini.page.wait_for("sku-attr-row[is='components/e-core/sku-attributes/row/index']", 1)
        if sku_flag:
            sku_btns = self.mini.page.get_elements("sku-attr-row[is='components/e-core/sku-attributes/row/index']")
            complete_flag = False
            for i in range(0, len(sku_btns), 1):
                sku_btns_two = sku_btns[i].get_elements(
                    "attr-item[is='components/e-core/sku-panel/attr-row/attr-item/index'] >>> view")
                if len(sku_btns) == 0 and len(sku_btns_two) == 0:
                    break
                for j in range(0, len(sku_btns_two), 1):
                    if sku_btns_two[j].outer_wxml.__contains__('disabled'):
                        continue
                    else:
                        sku_btns_two[j].click()
                        if i == len(sku_btns) - 1:
                            complete_flag = True
                        break
                if complete_flag:
                    break
        return self

    """
    直播间提交购买
    """

    def submit_to_buy(self):
        # 提交订单
        time.sleep(1)
        self.select_address()
        self.select_time()
        time.sleep(3)
        submit_btn = self.mini.page.get_element("view.submit-bar-index--submit-bar__submit-btn")
        '''
        submit_btn.click()
        # 微信支付
        self.mini.page.wait_for("view.zan-pay__method.method-index--zan-pay__method.zan-pay__method--hairline")
        wechat_pay_button = self.mini.page.get_element("view.zan-pay__method.method-index--zan-pay__method.zan-pay__method--hairline")
        # wechat_pay_button.click()
        '''
        return self

    """
    选择提货地址
    """

    def select_address(self):
        select_add_flag = self.mini.page.wait_for("view.base-address-index--address__arrow.van-icon.dist-icon-index--van-icon.van-icon-arrow")
        if select_add_flag:
            select_add_element = self.mini.page.get_element("view.base-address-index--address__arrow.van-icon.dist-icon-index--van-icon.van-icon-arrow")
            select_add_element.click()
            if self.mini.page.wait_for("view.address-select-index--select-icon"):
                address_list = self.mini.page.get_elements("view.address-select-index--select-icon")
                address_list[0].click()
            elif self.mini.page.wait_for("view.address-select-index--address_info"):
                address_list = self.mini.page.get_elements("view.address-select-index--address_info")
                address_list[0].click()
            else:
                address_list = self.mini.page.get_elements("view.address-select-index--address_info")
                address_list[0].click()
        return self

    """
    选择提货时间
    """

    def select_time(self):
        select_time_flag = self.mini.page.wait_for(
            "view.dist-cell-index--van-cell__right-icon.van-icon.dist-icon-index--van-icon")
        if select_time_flag:
            select_time_elements = self.mini.page.get_elements(
                "view.dist-cell-index--van-cell__right-icon.van-icon.dist-icon-index--van-icon")
            select_time_elements[1].click()
            time_complete_btn = self.mini.page.get_element("view.date-select-index--btn")
            time_complete_btn.click()
        return self